<?php

namespace app\admin\controller;

use app\BaseController;
use app\model\AdminModel;
use app\model\UserModel;
use think\App;

class Base extends BaseController
{

    public function __construct(App $app)
    {
        parent::__construct($app);

    }


    /*
     * 输出成功结果
     * */
    public function succData($total = 0,$data=[],$msg = "success")
    {
        $response['code'] = 1;
        $response['total'] = $total;
        $response['data'] = $data;
        $response['msg'] = $msg;
        echo json_encode($response,256);
        die;
    }
    /*
     * 输出成功结果
     * */
    public function succ($data=[],$msg = "success")
    {
        $response['code'] = 1;
        $response['data'] = $data;
        $response['msg'] = $msg;
        echo json_encode($response,256);
        die;
    }


    /*
      * 输出成功结果
      * */
    public function fail($msg = "操作失败")
    {
        $response['code'] = 0;
        $response['msg'] = $msg;
        echo json_encode($response,256);
        die;
    }


    /*
  * 未登录
  * */
    public function noLogin(){
        $response['code'] = -2;
        $response['msg'] = "请先登录";
        echo json_encode($response,256);
        die;
    }





    /*
     * 检查是否登录
     * */
    public function isLogin(){
        $token = request()->header('token');
        if(empty($token)){
            return $this->noLogin();
        }

        $userInfo =  (new AdminModel())->getCache($token);
        $userInfo = json_decode($userInfo,true);

        if(empty($userInfo) || empty($userInfo["aid"])){
            return $this->noLogin();
        }

        return $userInfo["aid"];
    }
}
